home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / CUGUK / GAMES / C014.ZIP / LARN_SRC.ZIP / SCORES.C < prev    next >
C/C++ Source or Header  |  1993-11-17  |  26KB  |  704 lines

  1. /* scores.c
  2.  *
  3.  *  readboard()     Function to read in the scoreboard into a static buffer
  4.  *  writeboard()    Function to write the scoreboard from readboard()'s buffer
  5.  *  makeboard()     Function to create a new scoreboard (wipe out old one)
  6.  *  hashewon()   Function to return 1 if player has won a game before, else 0
  7.  *  long paytaxes(x)     Function to pay taxes if any are due
  8.  *  winshou()       Subroutine to print out the winning scoreboard
  9.  *  shou(x)         Subroutine to print out the non-winners scoreboard
  10.  *  showscores()        Function to show the scoreboard on the terminal
  11.  *  showallscores() Function to show scores and the iven lists that go with them
  12.  *  sortboard()     Function to sort the scoreboard
  13.  *  newscore(score, whoo, whyded, winner)   Function to add entry to scoreboard
  14.  *  new1sub(score,i,whoo,taxes)           Subroutine to put player into a
  15.  *  new2sub(score,i,whoo,whyded)          Subroutine to put player into a
  16.  *  died(x)     Subroutine to record who played larn, and what the score was
  17.  *  diedsub(x) Subroutine to print out a line showing player when he is killed
  18.  *  diedlog()   Subroutine to read a log file and print it out in ascii format
  19.  *  getplid(name)       Function to get players id # from id file
  20.  */
  21. #ifdef VMS
  22. # include <types.h>
  23. # include <stat.h>
  24. #else
  25. # include <sys/types.h>
  26. # ifndef MSDOS
  27. #  include <sys/times.h>
  28. # endif
  29. # include <sys/stat.h>
  30. #endif
  31. #include "header.h"
  32. #include "larndefs.h"
  33. #include "monsters.h"
  34. #include "objects.h"
  35. #include "player.h"
  36.  
  37. struct scofmt           /*  This is the structure for the scoreboard        */
  38.     {
  39.     long score;         /* the score of the player                          */
  40.     long suid;          /* the user id number of the player                 */
  41.     short what;         /* the number of the monster that killed player     */
  42.     short level;        /* the level player was on when he died             */
  43.     short hardlev;      /* the level of difficulty player played at         */
  44.     short order;        /* the relative ordering place of this entry        */
  45.     char who[40];       /* the name of the character                        */
  46.     char sciv[26][2];   /* this is the inventory list of the character      */
  47.     };
  48. struct wscofmt          /* This is the structure for the winning scoreboard */
  49.     {
  50.     long score;         /* the score of the player                          */
  51.     long timeused;      /* the time used in mobuls to win the game          */
  52.     long taxes;         /* taxes he owes to LRS                             */
  53.     long suid;          /* the user id number of the player                 */
  54.     short hardlev;      /* the level of difficulty player played at         */
  55.     short order;        /* the relative ordering place of this entry        */
  56. # ifndef MAIL           /* dgk */
  57.     char hasmail;       /* 1 if mail is to be read, 0 otherwise */
  58. # endif
  59.     char who[40];       /* the name of the character                        */
  60.     };
  61.  
  62. struct log_fmt          /* 102 bytes struct for the log file                */
  63.     {
  64.     long score;         /* the players score                                */
  65.     long diedtime;      /* time when game was over                          */
  66.     short cavelev;      /* level in caves                                   */
  67.     short diff;         /* difficulty player played at                      */
  68. #ifdef EXTRA
  69.     long elapsedtime;   /* real time of game in seconds                     */
  70.     long bytout;        /* bytes input and output                           */
  71.     long bytin;
  72.     long moves;         /* number of moves made by player                   */
  73.     short ac;           /* armor class of player                            */
  74.     short hp,hpmax;     /* players hitpoints                                */
  75.     short cputime;      /* cpu time needed in seconds                       */
  76.     short killed,spused;/* monsters killed and spells cast                  */
  77.     short usage;        /* usage of the cpu in %                            */
  78.     short lev;          /* player level                                     */
  79. #endif
  80.     char who[12];       /* player name                                      */
  81.     char what[46];      /* what happened to player                          */
  82.     };
  83.  
  84. static struct scofmt sco[SCORESIZE];    /* the structure for the scoreboard  */
  85. static struct wscofmt winr[SCORESIZE];  /* struct for the winning scoreboard */
  86. static struct log_fmt logg;             /* structure for the log file        */
  87. static char *whydead[] = {
  88.     "quit", "suspended", "self - annihilated", "shot by an arrow",
  89.     "hit by a dart", "fell into a pit", "fell into a bottomless pit",
  90.     "a winner", "trapped in solid rock", "killed by a missing save file",
  91.     "killed by an old save file", "caught by the greedy cheater checker trap",
  92.     "killed by a protected save file","killed his family and committed suicide",
  93.     "erased by a wayward finger", "fell through a bottomless trap door",
  94.     "fell through a trap door", "drank some poisonous water",
  95.     "fried by an electric shock", "slipped on a volcano shaft",
  96.     "killed by a stupid act of frustration", "attacked by a revolting demon",
  97.     "hit by his own magic", "demolished by an unseen attacker",
  98.     "fell into the dreadful sleep", "killed by an exploding chest",
  99. /*26*/  "killed by a missing maze data file", "annihilated in a sphere",
  100.     "died a post mortem death","wasted by a malloc() failure"
  101.     };
  102.  
  103.  
  104. /*
  105.  *  readboard()     Function to read in the scoreboard into a static buffer
  106.  *
  107.  *  returns -1 if unable to read in the scoreboard, returns 0 if all is OK
  108.  */
  109. static readboard()
  110.     {
  111.     if (lopen(scorefile)<0)
  112.       { lprcat("Can't read scoreboard\n"); lflush(); return(-1); }
  113.     lrfill((char*)sco,sizeof(sco));     lrfill((char*)winr,sizeof(winr));
  114.     lrclose();  lcreat((char*)0);  return(0);
  115.     }
  116.  
  117. /*
  118.  *  writeboard()    Function to write the scoreboard from readboard()'s buffer
  119.  *
  120.  *  returns -1 if unable to write the scoreboard, returns 0 if all is OK
  121.  */
  122. static writeboard()
  123.     {
  124.     set_score_output();
  125.     if (lcreat(scorefile)<0)
  126.       { lprcat("Can't write scoreboard\n"); lflush(); return(-1); }
  127.     lwrite((char*)sco,sizeof(sco));     lwrite((char*)winr,sizeof(winr));
  128.     lwclose();  lcreat((char*)0);  return(0);
  129.     }
  130.  
  131. /*
  132.  *  makeboard()         Function to create a new scoreboard (wipe out old one)
  133.  *
  134.  *  returns -1 if unable to write the scoreboard, returns 0 if all is OK
  135.  */
  136. makeboard()
  137.     {
  138.     register int i;
  139.     for (i=0; i<SCORESIZE; i++)
  140.         {
  141.         winr[i].taxes = winr[i].score = sco[i].score = 0;
  142.         winr[i].order = sco[i].order = i;
  143.         }
  144.     if (writeboard()) return(-1);
  145.     chmod(scorefile,0666);
  146.     return(0);
  147.     }
  148.  
  149. /*
  150.  *  hashewon()   Function to return 1 if player has won a game before, else 0
  151.  *
  152.  *  This function also sets c[HARDGAME] to appropriate value -- 0 if not a
  153.  *  winner, otherwise the next level of difficulty listed in the winners
  154.  *  scoreboard.  This function also sets outstanding_taxes to the value in
  155.  *  the winners scoreboard.
  156.  */
  157. hashewon()
  158.     {
  159.     register int i;
  160.     c[HARDGAME] = 0;
  161.     if (readboard() < 0) return(0); /* can't find scoreboard */
  162.     for (i=0; i<SCORESIZE; i++) /* search through winners scoreboard */
  163.        if (winr[i].suid == userid)
  164.           if (winr[i].score > 0)
  165.             {
  166.             c[HARDGAME]=winr[i].hardlev+1;  outstanding_taxes=winr[i].taxes;
  167.             return(1);
  168.             }
  169.     return(0);
  170.     }
  171.  
  172. # ifndef MAIL           /* dgk */
  173. checkmail()
  174. {
  175.     register int    i;
  176.     long        gold, taxes;
  177.  
  178.     if (readboard() < 0)
  179.         return;         /* can't find scoreboard */
  180.     for (i = 0; i < SCORESIZE; i++) /* search through winners scoreboard */
  181.         if (winr[i].suid == userid
  182.         &&  winr[i].score > 0
  183.         &&  winr[i].hasmail) {
  184.             winr[i].hasmail = 0;
  185.             gold = taxes = winr[i].taxes;
  186.             writeboard();
  187.  
  188.             /* Intuit the amount of gold -- should have changed
  189.              * the score file, but ...  TAXRATE is an fraction.
  190.              */
  191.             while ((gold * TAXRATE) < taxes)
  192.                 gold += taxes;
  193.             readmail(gold);
  194.         }
  195. }
  196. # endif
  197.  
  198.  
  199. /*
  200.  *  long paytaxes(x)         Function to pay taxes if any are due
  201.  *
  202.  *  Enter with the amount (in gp) to pay on the taxes.
  203.  *  Returns amount actually paid.
  204.  */
  205. long paytaxes(x)
  206.     long x;
  207.     {
  208.     register int i;
  209.     register long amt;
  210.     if (x<0) return(0L);
  211.     if (readboard()<0) return(0L);
  212.     for (i=0; i<SCORESIZE; i++)
  213.         if (winr[i].suid == userid) /* look for players winning entry */
  214.             if (winr[i].score>0) /* search for a winning entry for the player */
  215.                 {
  216.                 amt = winr[i].taxes;
  217.                 if (x < amt) amt=x;     /* don't overpay taxes (Ughhhhh) */
  218.                 winr[i].taxes -= amt;
  219.                 outstanding_taxes -= amt;
  220.                 if (writeboard()<0) return(0);
  221.                 return(amt);
  222.                 }
  223.     return(0L); /* couldn't find user on winning scoreboard */
  224.     }
  225.  
  226. /*
  227.  *  winshou()       Subroutine to print out the winning scoreboard
  228.  *
  229.  *  Returns the number of players on scoreboard that were shown 
  230.  */
  231. static winshou()
  232.     {
  233.     register struct wscofmt *p;
  234.     register int i,j,count;
  235.     for (count=j=i=0; i<SCORESIZE; i++) /* is there anyone on the scoreboard? */
  236.         if (winr[i].score != 0)
  237.             { j++; break; }
  238.     if (j)
  239.         {
  240.         lprcat("\n  Score    Difficulty   Time Needed   Larn Winners List\n");
  241.  
  242.         for (i=0; i<SCORESIZE; i++) /* this loop is needed to print out the */
  243.           for (j=0; j<SCORESIZE; j++) /* winners in order */
  244.             {
  245.             p = &winr[j];   /* pointer to the scoreboard entry */
  246.             if (p->order == i)
  247.                 {
  248.                 if (p->score)
  249.                     {
  250.                     count++;
  251.                     lprintf("%10d     %2d      %5d Mobuls   %s \n",
  252.                     (long)p->score,(long)p->hardlev,(long)p->timeused,p->who);
  253.                     }
  254.                 break;
  255.                 }
  256.             }
  257.         }
  258.     return(count);  /* return number of people on scoreboard */
  259.     }
  260.  
  261. /*
  262.  *  shou(x)         Subroutine to print out the non-winners scoreboard
  263.  *      int x;
  264.  *
  265.  *  Enter with 0 to list the scores, enter with 1 to list inventories too
  266.  *  Returns the number of players on scoreboard that were shown 
  267.  */
  268. static shou(x)
  269.     int x;
  270.     {
  271.     register int i,j,n,k;
  272.     int count;
  273.     for (count=j=i=0; i<SCORESIZE; i++) /* is the scoreboard empty? */
  274.         if (sco[i].score!= 0)
  275.             { j++; break; }
  276.     if (j)
  277.         {
  278.         lprcat("\n   Score   Difficulty   Larn Visitor Log\n");
  279.         for (i=0; i<SCORESIZE; i++) /* be sure to print them out in order */
  280.           for (j=0; j<SCORESIZE; j++)
  281.             if (sco[j].order == i)
  282.                 {
  283.                 if (sco[j].score)
  284.                     {
  285.                     count++;
  286.                     lprintf("%10d     %2d       %s ",
  287.                         (long)sco[j].score,(long)sco[j].hardlev,sco[j].who);
  288.                     if (sco[j].what < 256) lprintf("killed by a %s",monster[sco[j].what].name);
  289.                         else lprintf("%s",whydead[sco[j].what - 256]);
  290.                     if (x != 263) lprintf(" on %s",levelname[sco[j].level]);
  291.                     if (x)
  292.                         {
  293.                         for (n=0; n<26; n++) { iven[n]=sco[j].sciv[n][0]; ivenarg[n]=sco[j].sciv[n][1]; }
  294.                         for (k=1; k<99; k++)
  295.                           for (n=0; n<26; n++)
  296.                             if (k==iven[n]) show3(n);
  297.                         lprcat("\n\n");
  298.                         }
  299.                     else lprc('\n');
  300.                     }
  301.                 j=SCORESIZE;
  302.                 }
  303.         }
  304.     return(count);  /* return the number of players just shown */
  305.     }
  306.  
  307. /*
  308.  *  showscores()        Function to show the scoreboard on the terminal
  309.  *
  310.  *  Returns nothing of value
  311.  */
  312. static char esb[] = "The scoreboard is empty.\n";
  313. showscores()
  314.     {
  315.     register int i,j;
  316.     lflush();  lcreat((char*)0);  if (readboard()<0) return;
  317.     i=winshou();    j=shou(0);
  318.     if (i+j == 0) lprcat(esb); else lprc('\n');
  319.     lflush();
  320.     }
  321.  
  322. /*
  323.  *  showallscores() Function to show scores and the iven lists that go with them
  324.  *
  325.  *  Returns nothing of value
  326.  */
  327. showallscores()
  328.     {
  329.     register int i,j;
  330.     lflush();  lcreat((char*)0);  if (readboard()<0) return;
  331.     c[WEAR] = c[WIELD] = c[SHIELD] = -1;  /* not wielding or wearing anything */
  332.     for (i=0; i<MAXPOTION; i++) potionname[i][0]=' ';
  333.     for (i=0; i<MAXSCROLL; i++) scrollname[i][0]=' ';
  334.     i=winshou();  j=shou(1);
  335.     if (i+j==0) lprcat(esb); else lprc('\n');
  336.     lflush();
  337.     }
  338.  
  339. /*
  340.  *  sortboard()     Function to sort the scoreboard
  341.  *
  342.  *  Returns 0 if no sorting done, else returns 1
  343.  */
  344. static sortboard()
  345.     {
  346.     register int i,j,pos;
  347.     long jdat;
  348.     for (i=0; i<SCORESIZE; i++) sco[i].order = winr[i].order = -1;
  349.     pos=0;  while (pos < SCORESIZE)
  350.         {
  351.         jdat=0;
  352.         for (i=0; i<SCORESIZE; i++)
  353.             if ((sco[i].order < 0) && (sco[i].score >= jdat))
  354.                 { j=i;  jdat=sco[i].score; }
  355.         sco[j].order = pos++;
  356.         }
  357.     pos=0;  while (pos < SCORESIZE)
  358.         {
  359.         jdat=0;
  360.         for (i=0; i<SCORESIZE; i++)
  361.             if ((winr[i].order < 0) && (winr[i].score >= jdat))
  362.                 { j=i;  jdat=winr[i].score; }
  363.         winr[j].order = pos++;
  364.         }
  365.     return(1);
  366.     }
  367.  
  368. /*
  369.  *  newscore(score, whoo, whyded, winner)   Function to add entry to scoreboard
  370.  *      int score, winner, whyded;
  371.  *      char *whoo;
  372.  *
  373.  *  Enter with the total score in gp in score,  players name in whoo,
  374.  *      died() reason # in whyded, and TRUE/FALSE in winner if a winner
  375.  *  ex.     newscore(1000, "player 1", 32, 0);
  376.  */
  377. static newscore(score, whoo, whyded, winner)
  378.     long score;
  379.     int winner, whyded;
  380.     char *whoo;
  381.     {
  382.     register int i;
  383.     long taxes;
  384.     if (readboard() < 0) return;    /*  do the scoreboard   */
  385.     /* if a winner then delete all non-winning scores */
  386.     if (cheat) winner=0;    /* if he cheated, don't let him win */
  387.     if (winner)
  388.         {
  389.         for (i=0; i<SCORESIZE; i++) if (sco[i].suid == userid) sco[i].score=0;
  390.         taxes = score*TAXRATE;
  391.         score += 100000*c[HARDGAME];    /* bonus for winning */
  392.     /* if he has a slot on the winning scoreboard update it if greater score */
  393.         for (i=0; i<SCORESIZE; i++) if (winr[i].suid == userid)
  394.                 { new1sub(score,i,whoo,taxes); return; }
  395.     /* he had no entry. look for last entry and see if he has a greater score */
  396.         for (i=0; i<SCORESIZE; i++) if (winr[i].order == SCORESIZE-1)
  397.                 { new1sub(score,i,whoo,taxes); return; }
  398.         }
  399.     else if (!cheat) /* for not winning scoreboard */
  400.         {
  401.     /* if he has a slot on the scoreboard update it if greater score */
  402.         for (i=0; i<SCORESIZE; i++) if (sco[i].suid == userid)
  403.                 { new2sub(score,i,whoo,whyded); return; }
  404.     /* he had no entry. look for last entry and see if he has a greater score */
  405.         for (i=0; i<SCORESIZE; i++) if (sco[i].order == SCORESIZE-1)
  406.                 { new2sub(score,i,whoo,whyded); return; }
  407.         }
  408.     }
  409.  
  410. /*
  411.  *  new1sub(score,i,whoo,taxes)       Subroutine to put player into a 
  412.  *      int score,i,whyded,taxes;         winning scoreboard entry if his score
  413.  *      char *whoo;                       is high enough
  414.  *
  415.  *  Enter with the total score in gp in score,  players name in whoo,
  416.  *      died() reason # in whyded, and TRUE/FALSE in winner if a winner
  417.  *      slot in scoreboard in i, and the tax bill in taxes.
  418.  *  Returns nothing of value
  419.  */
  420. static new1sub(score,i,whoo,taxes)
  421.     long score,taxes;
  422.     int i;
  423.     char *whoo;
  424.     {
  425.     register struct wscofmt *p;
  426.     p = &winr[i];
  427.     p->taxes += taxes;
  428.     if ((score >= p->score) || (c[HARDGAME] > p->hardlev))
  429.         {
  430.         strcpy(p->who,whoo);        p->score=score;
  431.         p->hardlev=c[HARDGAME];     p->suid=userid;
  432.         p->timeused=gtime/100;
  433. # ifndef MAIL           /* dgk */
  434.         p->hasmail = 1;
  435. # endif
  436.         }
  437.     }
  438.  
  439. /*
  440.  *  new2sub(score,i,whoo,whyded)          Subroutine to put player into a 
  441.  *      int score,i,whyded,taxes;         non-winning scoreboard entry if his
  442.  *      char *whoo;                       score is high enough
  443.  *
  444.  *  Enter with the total score in gp in score,  players name in whoo,
  445.  *      died() reason # in whyded, and slot in scoreboard in i.
  446.  *  Returns nothing of value
  447.  */
  448. static new2sub(score,i,whoo,whyded)
  449.     long score;
  450.     int i,whyded;
  451.     char *whoo;
  452.     {
  453.     register int j;
  454.     register struct scofmt *p;
  455.     p = &sco[i];
  456.     if ((score >= p->score) || (c[HARDGAME] > p->hardlev))
  457.         {
  458.         strcpy(p->who,whoo);  p->score=score;
  459.         p->what=whyded;       p->hardlev=c[HARDGAME];
  460.         p->suid=userid;       p->level=level;
  461.         for (j=0; j<26; j++)
  462.             { p->sciv[j][0]=iven[j]; p->sciv[j][1]=ivenarg[j]; }
  463.         }
  464.     }
  465.  
  466. /*
  467.  *  died(x)     Subroutine to record who played larn, and what the score was
  468.  *      int x;
  469.  *
  470.  *  if x < 0 then don't show scores
  471.  *  died() never returns! (unless c[LIFEPROT] and a reincarnatable death!)
  472.  *
  473.  *      < 256   killed by the monster number
  474.  *      256     quit
  475.  *      257     suspended
  476.  *      258     self - annihilated
  477.  *      259     shot by an arrow
  478.  *      260     hit by a dart
  479.  *      261     fell into a pit
  480.  *      262     fell into a bottomless pit
  481.  *      263     a winner
  482.  *      264     trapped in solid rock
  483.  *      265     killed by a missing save file
  484.  *      266     killed by an old save file
  485.  *      267     caught by the greedy cheater checker trap
  486.  *      268     killed by a protected save file
  487.  *      269     killed his family and killed himself
  488.  *      270     erased by a wayward finger
  489.  *      271     fell through a bottomless trap door
  490.  *      272     fell through a trap door
  491.  *      273     drank some poisonous water
  492.  *      274     fried by an electric shock
  493.  *      275     slipped on a volcano shaft
  494.  *      276     killed by a stupid act of frustration
  495.  *      277     attacked by a revolting demon
  496.  *      278     hit by his own magic
  497.  *      279     demolished by an unseen attacker
  498.  *      280     fell into the dreadful sleep
  499.  *      281     killed by an exploding chest
  500.  *      282     killed by a missing maze data file
  501.  *      283     killed by a sphere of annihilation
  502.  *      284     died a post mortem death
  503.  *      285     malloc() failure
  504.  *      300     quick quit -- don't put on scoreboard
  505.  */
  506.  
  507. static int scorerror;
  508. died(x)
  509.     int x;
  510.     {
  511.     register int f,win;
  512.     char ch,*mod;
  513.     long zzz,i;
  514. # ifdef EXTRA
  515.     struct tms cputime;
  516. # endif
  517.     if (c[LIFEPROT]>0) /* if life protection */
  518.         {
  519.         switch((x>0) ? x : -x)
  520.             {
  521.             case 256: case 257: case 262: case 263: case 265: case 266:
  522.             case 267: case 268: case 269: case 271: case 282: case 284:
  523.             case 285: case 300:  goto invalid; /* can't be saved */
  524.             };
  525.         --c[LIFEPROT]; c[HP]=1; --c[CONSTITUTION];
  526.         cursors(); lprcat("\nYou feel wiiieeeeerrrrrd all over! "); beep();
  527.         lflush();  sleep(4);
  528.         return; /* only case where died() returns */
  529.         }
  530. invalid:
  531.     clearvt100();  lflush();  f=0;
  532.     if (ckpflag) unlink(ckpfile);   /* remove checkpoint file if used */
  533. # ifdef MSDOS
  534.     if (swapfd) {
  535.         close(swapfd);
  536.         (void) unlink(swapfile);/* Remove swapfile */
  537.     }
  538.     unsetraw();
  539. # endif
  540.     if (x<0) { f++; x = -x; }   /* if we are not to display the scores */
  541.     if ((x == 300) || (x == 257))  exit();  /* for quick exit or saved game */
  542.     if (x == 263)  win = 1;  else  win = 0;
  543.     c[GOLD] += c[BANKACCOUNT];   c[BANKACCOUNT] = 0;
  544.         /*  now enter the player at the end of the scoreboard */
  545.     newscore(c[GOLD], logname, x, win);
  546.     diedsub(x); /* print out the score line */  lflush();
  547.  
  548.     set_score_output();
  549.     if ((wizard == 0) && (c[GOLD] > 0))     /*  wizards can't score     */
  550.         {
  551.         if (lappend(logfile)<0)  /* append to file */
  552.             {
  553.             if (lcreat(logfile)<0) /* and can't create new log file */
  554.                 {
  555.                 lcreat((char*)0);
  556.                 lprcat("\nCan't open record file:  I can't post your score.\n");
  557.                 sncbr();  resetscroll();  lflush();  exit();
  558.                 }
  559.             chmod(logfile,0666);
  560.             }
  561.         strcpy(logg.who,loginname);
  562.         logg.score = c[GOLD];       logg.diff = c[HARDGAME];
  563.         if (x < 256)
  564.             {
  565.             ch = *monster[x].name;
  566.             if (ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
  567.                 mod="an";  else mod="a";
  568.             sprintf(logg.what,"killed by %s %s",mod,monster[x].name);
  569.             }
  570.         else sprintf(logg.what,"%s",whydead[x - 256]);
  571.         logg.cavelev=level;
  572.         time(&zzz);   /* get cpu time -- write out score info */
  573.         logg.diedtime=zzz;
  574. #ifdef EXTRA
  575.         times(&cputime);  /* get cpu time -- write out score info */
  576.         logg.cputime = i = (cputime.tms_utime + cputime.tms_stime)/60 + c[CPUTIME];
  577.         logg.lev=c[LEVEL];          logg.ac=c[AC];
  578.         logg.hpmax=c[HPMAX];        logg.hp=c[HP];
  579.         logg.elapsedtime=(zzz-initialtime+59)/60;
  580.         logg.usage=(10000*i)/(zzz-initialtime);
  581.         logg.bytin=c[BYTESIN];      logg.bytout=c[BYTESOUT];
  582.         logg.moves=c[MOVESMADE];    logg.spused=c[SPELLSCAST];
  583.         logg.killed=c[MONSTKILLED];
  584. #endif
  585.         lwrite((char*)&logg,sizeof(struct log_fmt));     lwclose();
  586.  
  587. /*  now for the scoreboard maintenance -- not for a suspended game  */
  588.         if (x != 257)
  589.             {
  590.             if (sortboard())  scorerror = writeboard();
  591.             }
  592.         }
  593.     if ((x==256) || (x==257) || (f != 0)) exit();
  594.     if (scorerror == 0) showscores();   /* if we updated the scoreboard */
  595. # ifdef MAIL
  596.     if (x == 263) mailbill();
  597. # endif
  598.     exit();
  599.     }
  600.  
  601. /*
  602.  *  diedsub(x) Subroutine to print out the line showing the player when he is killed
  603.  *      int x;
  604.  */
  605. static diedsub(x)
  606. int x;
  607.     {
  608.     register char ch,*mod;
  609.     lprintf("Score: %d, Diff: %d,  %s ",(long)c[GOLD],(long)c[HARDGAME],logname);
  610.     if (x < 256)
  611.         {
  612.         ch = *monster[x].name;
  613.         if (ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
  614.             mod="an";  else mod="a";
  615.         lprintf("killed by %s %s",mod,monster[x].name);
  616.         }
  617.     else lprintf("%s",whydead[x - 256]);
  618.     if (x != 263) lprintf(" on %s\n",levelname[level]);  else lprc('\n');
  619.     }
  620.  
  621. /*
  622.  *  diedlog()   Subroutine to read a log file and print it out in ascii format
  623.  */
  624. diedlog()
  625.     {
  626.     register int n;
  627.     register char *p;
  628.     struct stat stbuf;
  629.     lcreat((char*)0);
  630.     if (lopen(logfile)<0)
  631.         {
  632.         lprintf("Can't locate log file <%s>\n",logfile);
  633.         return;
  634.         }
  635.     if (fstat(fd,&stbuf) < 0)
  636.         {
  637.         lprintf("Can't  stat log file <%s>\n",logfile);
  638.         return;
  639.         }
  640.     for (n=stbuf.st_size/sizeof(struct log_fmt); n>0; --n)
  641.         {
  642.         lrfill((char*)&logg,sizeof(struct log_fmt));
  643.         p = ctime(&logg.diedtime); p[16]='\n'; p[17]=0;
  644.         lprintf("Score: %d, Diff: %d,  %s %s on %d at %s",(long)(logg.score),(long)(logg.diff),logg.who,logg.what,(long)(logg.cavelev),p+4);
  645. #ifdef EXTRA
  646.         if (logg.moves<=0) logg.moves=1;
  647.         lprintf("  Experience Level: %d,  AC: %d,  HP: %d/%d,  Elapsed Time: %d minutes\n",(long)(logg.lev),(long)(logg.ac),(long)(logg.hp),(long)(logg.hpmax),(long)(logg.elapsedtime));
  648.         lprintf("  CPU time used: %d seconds,  Machine usage: %d.%02d%%\n",(long)(logg.cputime),(long)(logg.usage/100),(long)(logg.usage%100));
  649.         lprintf("  BYTES in: %d, out: %d, moves: %d, deaths: %d, spells cast: %d\n",(long)(logg.bytin),(long)(logg.bytout),(long)(logg.moves),(long)(logg.killed),(long)(logg.spused));
  650.         lprintf("  out bytes per move: %d,  time per move: %d ms\n",(long)(logg.bytout/logg.moves),(long)((logg.cputime*1000)/logg.moves));
  651. #endif
  652.         }
  653.         lflush();  lrclose();  return;
  654.     }
  655.  
  656. #ifndef UIDSCORE
  657. /*
  658.  *  getplid(name)       Function to get players id # from id file
  659.  *
  660.  *  Enter with the name of the players character in name.
  661.  *  Returns the id # of the players character, or -1 if failure.
  662.  *  This routine will try to find the name in the id file, if its not there,
  663.  *  it will try to make a new entry in the file.  Only returns -1 if can't
  664.  *  find him in the file, and can't make a new entry in the file.
  665.  *  Format of playerids file:
  666.  *          Id # in ascii     \n     character name     \n   
  667.  */
  668. static int havepid= -1; /* playerid # if previously done */
  669. getplid(nam)
  670.     char *nam;
  671.     {
  672.     int fd7,high=999,no;
  673.     register char *p,*p2;
  674.     char name[80];
  675.     if (havepid != -1) return(havepid); /* already did it */
  676.     lflush();   /* flush any pending I/O */
  677.     sprintf(name,"%s\n",nam);   /* append a \n to name */
  678.     if (lopen(playerids) < 0)   /* no file, make it */
  679.         {
  680.         if ((fd7=creat(playerids,0666)) < 0)  return(-1); /* can't make it */
  681.         close(fd7);  goto addone;   /* now append new playerid record to file */
  682.         }
  683.     for (;;)    /* now search for the name in the player id file */
  684.         {
  685.         p = lgetl();  if (p==NULL) break;   /* EOF? */
  686.         no = atoi(p);   /* the id # */
  687.         p2= lgetl();  if (p2==NULL) break;  /* EOF? */
  688.         if (no>high) high=no;   /* accumulate highest id # */
  689.         if (strcmp(p2,name)==0) /* we found him */
  690.             {
  691.             return(no); /* his id number */
  692.             }
  693.         }
  694.     lrclose();
  695.     /* if we get here, we didn't find him in the file -- put him there */
  696. addone:
  697.     if (lappend(playerids) < 0) return(-1); /* can't open file for append */
  698.     lprintf("%d\n%s",(long)++high,name);  /* new id # and name */
  699.     lwclose();
  700.     lcreat((char*)0);   /* re-open terminal channel */
  701.     return(high);
  702.     }
  703. #endif UIDSCORE
  704.